package me.shurufa.activities;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import de.greenrobot.event.EventBus;
import me.shurufa.R;
import me.shurufa.net.BaseResponse;
import me.shurufa.net.NetworkConstants;
import me.shurufa.net.RequestServerTask;
import me.shurufa.utils.Constants;
import me.shurufa.utils.Global;
import me.shurufa.utils.HttpUtil;
import me.shurufa.utils.Settings;
import me.shurufa.utils.Utils;

/**
 * 修改邮箱
 *
 * @FileName: me.shurufa.activities.ModifyEmailActivity.java
 * @author: kHRYSTAL
 * @email: 723526676@qq.com
 * @date: 2016-01-19 17:12
 */
public class ModifyEmailActivity extends BaseActivity implements View.OnClickListener {
    @Bind(R.id.email_edit)
    EditText emailEdit;
    @Bind(R.id.modify_complete)
    Button modifyComplete;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        activity_layout_res = R.layout.activity_modify_email;
        super.onCreate(savedInstanceState);
        initUI();
    }

    private void initUI() {
        if (!TextUtils.isEmpty(Global.currentUser.email))
            emailEdit.setText(Global.currentUser.email.trim());
        titleTextView.setText(getString(R.string.modify_email));
        modifyComplete.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.modify_complete:
                if (isInputValid()){
                    final String email = emailEdit.getText().toString().trim();
                    new RequestServerTask<BaseResponse>(BaseResponse.class,this,getString(R.string.commit_pls_wait)){

                        @Override
                        protected String requestServer() {
                            Map<String, Object> params = new HashMap<>();
                            params.put("token", Global.currentUser.token);
                            params.put("email", email);
                            return HttpUtil.post(NetworkConstants.USER_MODIFY_EMAIL, params);
                        }

                        @Override
                        protected void onSuccess(BaseResponse result) {
                            Utils.showToast(R.string.commit_success);
                            //设置昵称为当前用户邮箱
                            Global.currentUser.email = email;
                            //覆盖Sp
                            Settings.delUser(String.valueOf(Global.currentUser.uid));
                            Settings.saveUser(Global.currentUser);
                            EventBus.getDefault().post(new Intent(Constants.ACTION_FRAGMENT_REFRESH));
                            finish();
                        }
                    }.setDefaultErrMsg(R.string.commit_failed).start();
                }
                break;
        }
    }

    /**
     * 验证昵称是否符合规范
     * @return
     */
    private boolean isInputValid(){
        String trim = emailEdit.getText().toString().trim();
        if (TextUtils.isEmpty(trim)){
            Utils.showToast(R.string.email_can_not_empty);
            return false;
        }
        if(!trim.matches("\\w+@\\w+\\.\\w+")){
            Utils.showToast(R.string.email_format_not_correct);
            return false;
        }
        return true;
    }
}
